import { _decorator, Component, instantiate, JsonAsset, Node, Prefab, SpriteAtlas } from 'cc';
import { ConfigWeapItemData } from '../ConfigShapItemData';
import { GameTools } from '../../../Utils/GameTools';
import { eventMgr } from '../../../Runtime/EventManager';
import { GameEvent } from '../../../Runtime/GameEvent';
import { PathData } from '../../../Runtime/GameData';
import { WeapItem } from '../Weap/WeapItem';
const { ccclass } = _decorator;

@ccclass('WeapManager')
export class WeapManager extends Component {
    private static instance: WeapManager;
    public static get Instance(): WeapManager {
        if (!WeapManager.instance) {
            WeapManager.instance = new WeapManager();
        }
        return WeapManager.instance;
    }

    //初始生成的武器个数
    private weapNum: number = 3;
    //图集资源
    private m_atals: SpriteAtlas = null;
    //预制体资源
    private pf_WeapItem: Prefab = null;

    private m_allWeapItemData: Map<number, ConfigWeapItemData> = new Map()//所有数据包括地图和武器

    public m_initWeapItemData: Map<number, ConfigWeapItemData> = new Map()//初始武器数据

    protected onDisable(): void {
        eventMgr.off(GameEvent.PLACE_WEAP_UPDATE, this.initWeapItemList)
    }
    get atals() {
        if (!this.m_atals) {
            this.initWeapData();
        }
        return this.m_atals;
    }

    public async init() {
        eventMgr.on(GameEvent.PLACE_WEAP_UPDATE, this.initWeapItemList, this)
        await this.initWeapData()
    }
    //读取武器数据
    public async initWeapData() {
        await GameTools.loadResAsync(PathData.ConfigShapItemData, JsonAsset).then((res: JsonAsset) => {
            let list = res.json
            for (const key in list) {
                this.m_allWeapItemData.set(list[key].key, list[key])
            }
        })

        await GameTools.loadResAsync(PathData.TP_BagUI, SpriteAtlas).then((res: SpriteAtlas) => {
            this.m_atals = res;
        })

        await GameTools.loadResAsync(PathData.pf_WeapItem, Prefab).then((res: Prefab) => {
            this.pf_WeapItem = res;
        })
    }
    //初始化生成武器数据3个等级为1的武器数据
    initWeapItemList(parentNode: Node) {
        //调试获取1级武器
        this.m_initWeapItemData.clear();
        let weaponIdData: Array<ConfigWeapItemData> = this.get3WeapItemsbyLevel();
        //TODO 生成格子武器

        for (let i = 0; i < weaponIdData.length; i++) {
            const element = weaponIdData[i];
            this.m_initWeapItemData.set(Number(element.key), element);

            let data = weaponIdData[i];
            let ItemData = new ConfigWeapItemData()
            ItemData.loadJson(data)

            const node: Node = instantiate(this.pf_WeapItem)
            const com: WeapItem = node.getComponent(WeapItem)
            com.setWeapParent(parentNode)
            com.createWeapItem(ItemData)
        }
    }

    //获得武器数组
    getWeapItemData(): Array<ConfigWeapItemData> {
        let data = [];
        let arr = Array.from(this.m_allWeapItemData.values());
        for (let i = 0; i < arr.length; i++) {
            const element = arr[i];
            if (element.isWeap) {
                data.push(element);
            }
        }
        return data;
    }
    //获得武器初级数组3个
    get3WeapItemsbyLevel() {
        let arr: Array<ConfigWeapItemData> = [];
        let data: Array<ConfigWeapItemData> = GameTools.getRandomArr(this.getWeapItemData());
        for (let i = 0; i < data.length; i++) {
            const element = data[i];
            if (element.level == 1 && element.isWeap) {
                arr.push(element);
                if (arr.length == this.weapNum) {//3个
                    break;
                }
            }

        }
        return arr;
    }
    /* 通过level检测是否有该等级的装备 */
    checkWeaponByLevelType(level: number, type: number) {
        let key = type * 1000 + level;
        if (this.m_allWeapItemData.has(key)) {
            return true;
        }
        return false;
    }

}
export const weapMgr = WeapManager.Instance;


